package com.cn.wanxi.servlet.plate;

import com.cn.wanxi.model.PlateModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.INewsPlateService;
import com.cn.wanxi.service.impl.NewsPlateServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/newsPlateAdd")
public class NewsPlateAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        0.解决乱码
        req.setCharacterEncoding("UTF-8");//请求
        resp.setCharacterEncoding("UTF-8");//响应
//        1.拿到页面传递过来的值
        String name = req.getParameter("name");
        String status = req.getParameter("status");

//        2.封装model
        PlateModel plateModel = new PlateModel();
        plateModel.setName(name);
        plateModel.setStatus("1".equals(status) ? 1 : 0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss sss");
        plateModel.setCreateTime(simpleDateFormat.format(new Date()));
//        3.调用服务逻辑层
        INewsPlateService iNewsPlateService=new NewsPlateServiceImpl();
        ResultModel resultModel=iNewsPlateService.add(plateModel);
//        4.返回数据
        JSONArray jsonArray = JSONArray.fromObject(resultModel);
        resp.getWriter().println(jsonArray);
    }
}
